package cn.ticket.db;

import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBConnect {
	
	private final String DBDRIVER = "com.mysql.jdbc.Driver" ;
	private final String  URL="jdbc:mysql://localhost:3306/ticket?useUnicode=true&amp;characterEncoding=UTF-8";
	private final String  USERNAME = "root" ;
	private final String PASSWORD = "123456" ;
	
	private  Connection con ;
	public DBConnect(){
		try {
			Class.forName(DBDRIVER ).newInstance() ;
		} catch (Exception e) {
			System.out.println("加载驱动失败."+e.getMessage()) ;
		}
	}
	public Connection DBConnection() {
		this.con = (this.con==null?this.getConnection():this.con) ;
		return this.con ;
	}
	
	public Connection getConnection(){
		try {
			this.con = DriverManager.getConnection(URL,USERNAME,PASSWORD) ;
		} catch (Exception ex) {
			System.out.println("连接数据库失败...."+ex.getMessage()) ;
		}
		return this.con ;
	}
	
	
//	public static void main(String[] args) {
//		Connection dbcn = new DBConnect().DBConnection() ;
//		System.out.println(dbcn==null) ;
//		try {
//			PreparedStatement pstmt =  dbcn.prepareStatement("insert into user(cre_Time) values(?)") ;
//			pstmt.setDate(1, new java.sql.Date(new Date().getTime())) ;
//			pstmt.execute();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	
	
}
